﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using OpenCvSharp;
using OpenCvSharp.Extensions;
using WpfCropImage;
using Window = System.Windows.Window;
namespace OpenCv
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {

        [DllImport("gdi32")]

        private static extern int DeleteObject(IntPtr o);

        /// <summary>

        /// Convert an Bitmap to a WPF BitmapSource. The result can be used in the Set Property of Image.Source

        /// </summary>

        /// <param name="image">Bitmap</param>

        /// <returns>The equivalent BitmapSource</returns>

        public static BitmapSource ToBitmapSource(System.Drawing.Bitmap image)

        {

            IntPtr ptr = image.GetHbitmap();//obtain the Hbitmap

            BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap

            (

                ptr,

                IntPtr.Zero,

                Int32Rect.Empty,

                System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()

            );

            DeleteObject(ptr);//release the HBitmap

            return bs;

        }

        public MainWindow()
        {
            InitializeComponent();
        }
        VideoCapture Capture;
        Bitmap bitmap;
        bool isopen;
        private void Load()
        {

            Capture = new VideoCapture(0);

            if (!Capture.IsOpened())
            {
                //无法打开摄像头
                return;
            }
            isopen = true;
            Task.Run(() =>
            {

                Mat frame = new Mat();
                while (isopen)
                {
                    Capture.Read(frame);
                    if (frame.Empty())
                    {
                        break;
                    }
                    Dispatcher.Invoke(() =>
                    {

                        bitmap = BitmapConverter.ToBitmap(frame);
                        img.Source = ToBitmapSource(bitmap);
                    });

                }
            });
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            isopen = false;
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Load();
        }
        /// <summary>
        /// 点击拍摄
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Capture.Release();

            try
            {
                var bitmap2 = (System.Drawing.Bitmap)bitmap.Clone();
                CutImageEx cutImageEx = new CutImageEx(bitmap2);
                cutImageEx.ShowDialog();
            }
            catch (Exception)
            {
                MessageBox.Show("裁剪失败，请重试！", "错误提示");
            }
            if (CutImageEx.resImage != null)
            {
                img.Source = ToBitmapSource(CutImageEx.resImage);
            }
        }
    }
}
